package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsUserService;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DtsUserServiceImpl implements DtsUserService {
    @Autowired
    DtsUserMapper userMapper;
    @Autowired
    StatMapper statMapper;
    @Override
    public int getTotal() {
        LambdaQueryWrapper<DtsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsUser::getDeleted,false);
        Integer count = userMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public List<DayStatis> findUserDayStatis() {
        return statMapper.findUserDayStatis();
    }
    @Override
    public List<Map> findAllUserDayStatis() {
        return statMapper.findAllUserDayStatis();
    }

    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String username, String mobile, String sort, String order) {
        IPage<DtsUser> iPage = new Page<>(page, limit);
        QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)){
            wrapper.like(username,username);
        }
        if (!StringUtils.isEmpty(mobile)) {
            wrapper.eq(mobile, mobile);
        }
        if ("desc".equals(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        userMapper.selectPage(iPage,wrapper);
        long total = iPage.getTotal();
        List<DtsUser> records = iPage.getRecords();
        Map<String, Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",records);
        return data;
    }
}
